package com.woniuxy.joylife.manager.controller;

import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylife.manager.entity.UnitEntity;
import com.woniuxy.joylife.manager.service.UnitService;
import com.woniuxy.joylife.manager.utils.PageUtils;
import com.woniuxy.joylife.manager.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 单元信息
 *
 * @author renxiang
 * @email 981405195@qq.com
 * @date 2020-05-13 02:47:54
 */
@RestController
@RequestMapping("manager/unit")
public class UnitController {
    @Autowired
    private UnitService unitService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manager:unit:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = unitService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{uniId}")
    //@RequiresPermissions("manager:unit:info")
    public R info(@PathVariable("uniId") String uniId){
		UnitEntity unit = unitService.getById(uniId);

        return R.ok().put("unit", unit);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("manager:unit:save")
    public R save(@RequestBody UnitEntity unit) throws Exception {
        unit.setUniId(KeyUtil.generateKey());
		if(!unitService.saveUnitEntity(unit)){
		    throw new Exception("插入失败，该单元号已存在");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("manager:unit:update")
    public R update(@RequestBody UnitEntity unit){
		unitService.updateById(unit);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("manager:unit:delete")
    public R delete(@RequestBody String[] uniIds){
		unitService.removeByIds(Arrays.asList(uniIds));

        return R.ok();
    }

}
